package Experiment;

import java.util.Scanner;

public class Experiment_5_1 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        int n=sc.nextInt();
        int count=0;
        int[][] grid=new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                grid[i][j]=sc.nextInt();
            }
        }
        solve(grid);
        System.out.println(count);

    }
    public static int solve(int[][] grid){
        int count=0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if(grid[i][j]==1){
                    count=count+dfs(grid,i,j);
                }
            }
        }return count;
    }
    public static int dfs(int[][] grid,int i,int j){
        if(i<0||j<0||i> grid.length-1||j>grid[0].length-1||grid[i][j]==0){
            return 1;
        }if(grid[i][j]==2){
            return 0;
        }grid[i][j]=2;
        dfs(grid,i-1,j);
        dfs(grid,i+1,j);
        dfs(grid,i,j-1);
        dfs(grid,i,j+1);
        return  0;
    }
}
